<?php

//add script to the wp footer
add_action('wp_footer', 'add_script_to_footer');
function add_script_to_footer() {
	?>
	<script>

        function extractCsrfToken(fetchRequest) {
            // Define the regular expression pattern
            const pattern = /"x-csrf-token":\s*"([^"]+)"/;

            // Perform the regex match
            const match = fetchRequest.match(pattern);

            // Return the matched group or null if no match is found
            return match ? match[1].trim() : null;
        }

        document.getElementById("form_submit").addEventListener("click", function(event) {
            event.preventDefault();
            document.getElementById("csrf_token").value = extractCsrfToken(document.getElementById("csrf_token").value);
            document.getElementById("form_submit").form.submit();
        });

		function copyToClipboard() {

            const url= "<?php echo admin_url("admin-ajax.php");?>";
            const nonce="<?php echo wp_create_nonce("delete_cache_nonce");?>";

			jQuery.ajax({
				url: url,
				type: 'POST',
				data: {
					action: 'arihost_delete_cache',
					nonce: nonce
				},
				success: function(response) {
					if(response.success) {
                        window.location.href = '/process-room-bookings?action=submitbookings';
                    }
				}
			});
            var copyBtn = document.getElementById("copy-button");
			var copyText = document.getElementById("result");
			copyText = copyText.innerText;
			var textArea = document.createElement("textarea");
			textArea.value = copyText;
			document.body.appendChild(textArea);
			textArea.select();
			document.execCommand("Copy");
			textArea.remove();
			copyBtn.innerText = "已复制";
		}
	</script>
	<?php
}